<?php

namespace App\Http\Controllers\API;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    /**
     * 检验客户是否存在
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        if ($request->get('customer_name')) {
            $user = User::where(['customer_name' => $request->get('customer_name')])->get()->toArray();
        }

//        if ($request->get('host_mechanism_name')) {
//            $data['host_mechanism_name'] = $request->get('host_mechanism_name');
//        }

        if ($request->get('customer_number') && empty($user)) {
            $user = User::where('customer_number', $request->get('customer_number'))->get()->toArray();
        }

        if(!$request->get('customer_name') && !$request->get('customer_number')) {
            return $this->failure('对不起！请填写信息！');
        }

        if ($user) {
            foreach ($user as $key => $value) {
                if ($value['type'] == 1) {
                    $str[] = '春暖花开';
                } elseif ($value['type'] == 2) {
                    $str[] = '“薪”有所属';
                } elseif ($value['type'] == 3) {
                    $str[] = '心意甄选';
                }
                $customer_name = $value['customer_name'];
            }
            $res = implode('、', $str);
            return $this->success(['res' => $res, 'customer_name' => $customer_name]);
        } else {
            return $this->failure('对不起！填写有误');
        }
    }

    /**
     * 添加信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(Request $request)
    {
        $result = $request->all();

        if (!$request->get('customer_name')) {
            return $this->failure('请携带参数！');
        }
        $data['customer_name'] = $request->get('customer_name');
        $user = User::where($data)->get()->toArray();

        foreach ($user as $key => $value) {

            $users = User::where('id', $value['id'])->first();
            $users->name = $result['name'];
            $users->phone = $result['phone'];
            $users->address = $result['address'];

            $users->save();
        }


        return $this->success('信息提交成功！');
    }
}
